package dao;

import utils.User;
import utils.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDao implements IUser {
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rst;
    private ConnectionManager connectionManager = new ConnectionManager();
    private  LoginDao loginDao=new LoginDao();
    public boolean insert(User user) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("INSERT INTO t_user(name,pass,sex,tel,address) VALUES (?,?,?,?,?)");
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getSex());
            ps.setString(4,user.getTel());
            ps.setString(5,user.getAddress());
            int i = ps.executeUpdate();
            //loginDao.insert(user);
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public boolean delete(int userid) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("DELETE FROM t_user where userid=?");
            ps.setInt(1,userid);
            int i = ps.executeUpdate();
            //loginDao.delete(userid);
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public boolean update(User user) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("UPDATE t_user set name=?,pass=?,sex=?,tel=?,address=? where userid=?");
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getSex());
            ps.setString(4,user.getTel());
            ps.setString(5,user.getAddress());
            ps.setInt(6,user.getUser_id());
            int i = ps.executeUpdate();
            //loginDao.update(user);
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public User findbyname(String name) {
        User user=null;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_user where name="+name);
            rst = ps.executeQuery();
            if(rst.next()){
                user = setuser(rst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return user;
    }

    public ArrayList<User> findAll() {
        ArrayList<User> users = new ArrayList<User>();
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_user");
            rst = ps.executeQuery();
            while (rst.next()){
                User user = setuser(rst);
                users.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return users;
    }
    public User setuser(ResultSet rst) throws Exception {
        User user = new User();
        user.setUser_id(rst.getInt("userid"));
        user.setUsername(rst.getString("name"));
        user.setPassword(rst.getString("pass"));
        user.setSex(rst.getString("sex"));
        user.setTel(rst.getString("tel"));
        user.setAddress(rst.getString("address"));
        return user;
    }
    public boolean checkName(String name)
    {
        
        boolean result = false;
        if(name == null)
            return result;
        try
        {
            con=connectionManager.getCon();
            String sql = "select name from t_user where name=?";
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
            rst = ps.executeQuery();
            if(rst.next())
            {
                result = true;
            }
        }
        catch(SQLException e)
        {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        finally
        {
            // 关闭连接
            connectionManager.closeAll(con, ps, rst);
        }
        System.out.println("tag="+result);
        return result;
    }
}
